package com.webchat.ugc.messaegqueue.consumer.rocketmq;

import com.webchat.common.util.JsonUtil;
import com.webchat.domain.vo.request.ChattingRequestVO;
import com.webchat.domain.vo.request.mess.ChatMessageRequestVO;
import com.webchat.ugc.service.ChatMessageService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 刷新用户对话列表缓存：添加好友、订阅工作号、图文对话、机器人对话、音视频等完成后刷新。
 *
 * 这里消息消费不需要走广播，普通队列消费即可
 */
@Component
@RocketMQMessageListener(consumerGroup = "web_chat", topic = "queue_chatting_list_refresh")
public class RefreshChattingRocketMQConsumer implements RocketMQListener<String> {

    @Autowired
    private ChatMessageService chatMessageService;

    /**
     * 处理来自IM 对话相关消息
     *
     * @param message
     */
    @Override
    public void onMessage(String message) {
        System.out.println("Received message: " + message);
        ChatMessageRequestVO data = JsonUtil.fromJson(message, ChatMessageRequestVO.class);
        if (data == null) {
            return;
        }
        chatMessageService.addChattingList(ChattingRequestVO.builder()
                                            .account(data.getSenderId())
                                            .chatAccount(data.getReceiverId())
                                            .lastChatTime(data.getTime() == null ? System.currentTimeMillis() : data.getTime())
                                            .build());
    }
}